package com.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.entity.RepairmanOrder;
import com.repair.entity.RepairOrder;
import com.repair.mapper.RepairmanOrderMapper;
import com.repair.service.RepairmanOrderService;
import com.repair.service.RepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class RepairmanOrderServiceImpl extends ServiceImpl<RepairmanOrderMapper, RepairmanOrder> implements RepairmanOrderService {

    @Autowired
    private RepairOrderService repairOrderService;

    @Override
    public List<RepairmanOrder> getRepairmanOrders(Long repairmanId) {
        return baseMapper.getRepairmanOrders(repairmanId);
    }

    @Override
    public RepairmanOrder getOrderDetail(Long orderId) {
        return baseMapper.getOrderDetail(orderId);
    }

    @Override
    @Transactional
    public void acceptOrder(Long repairmanId, Long orderId, String remark) {
        // 检查订单是否存在且状态为待接单
        RepairOrder order = repairOrderService.getById(orderId);
        if (order == null || order.getStatus() != 0) {
            throw new RuntimeException("订单不存在或状态不正确");
        }

        // 检查是否已经被其他维修员接单
        LambdaQueryWrapper<RepairmanOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairmanOrder::getOrderId, orderId)
               .eq(RepairmanOrder::getStatus, 1);
        if (count(wrapper) > 0) {
            throw new RuntimeException("该订单已被其他维修员接单");
        }

        // 创建维修员接单记录
        RepairmanOrder repairmanOrder = new RepairmanOrder();
        repairmanOrder.setRepairmanId(repairmanId);
        repairmanOrder.setOrderId(orderId);
        repairmanOrder.setStatus(1); // 已接单
        repairmanOrder.setRemark(remark);
        repairmanOrder.setCreateTime(LocalDateTime.now());
        repairmanOrder.setUpdateTime(LocalDateTime.now());
        save(repairmanOrder);

        // 更新订单状态
        order.setStatus(1); // 已接单
        order.setRepairmanId(repairmanId);
        repairOrderService.updateById(order);
    }

    @Override
    @Transactional
    public void completeOrder(Long repairmanId, Long orderId) {
        // 检查维修员接单记录
        LambdaQueryWrapper<RepairmanOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairmanOrder::getRepairmanId, repairmanId)
               .eq(RepairmanOrder::getOrderId, orderId)
               .eq(RepairmanOrder::getStatus, 1);
        RepairmanOrder repairmanOrder = getOne(wrapper);
        if (repairmanOrder == null) {
            throw new RuntimeException("订单不存在或状态不正确");
        }

        // 更新维修员接单状态
        repairmanOrder.setStatus(2); // 已完成
        repairmanOrder.setUpdateTime(LocalDateTime.now());
        updateById(repairmanOrder);

        // 更新订单状态
        RepairOrder order = repairOrderService.getById(orderId);
        order.setStatus(2); // 已完成
        repairOrderService.updateById(order);
    }

    @Override
    @Transactional
    public void cancelOrder(Long repairmanId, Long orderId) {
        // 检查维修员接单记录
        LambdaQueryWrapper<RepairmanOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairmanOrder::getRepairmanId, repairmanId)
               .eq(RepairmanOrder::getOrderId, orderId)
               .eq(RepairmanOrder::getStatus, 1);
        RepairmanOrder repairmanOrder = getOne(wrapper);
        if (repairmanOrder == null) {
            throw new RuntimeException("订单不存在或状态不正确");
        }

        // 更新维修员接单状态
        repairmanOrder.setStatus(3); // 已取消
        repairmanOrder.setUpdateTime(LocalDateTime.now());
        updateById(repairmanOrder);

        // 更新订单状态
        RepairOrder order = repairOrderService.getById(orderId);
        order.setStatus(0); // 待接单
        order.setRepairmanId(null);
        repairOrderService.updateById(order);
    }
} 